/**
 * 2010(c) Copyright Oceansoft Information System Co.,LTD. All rights reserved.
 * <p>
 * Compile: JDK 1.6+
 * <p>
 * 版权所有(C)：江苏欧索软件有限公司
 * <p>
 * 公司名称：江苏欧索软件有限公司
 * <p>
 * 公司地址：中国苏州科技城青山路1号
 * <p>
 * 网址: http://www.oceansoft.com.cn
 * <p>
 * 作者: 090922(陈伟)
 * <p>
 * 文件名: com.oceansoft.mobile.econsole.modules.api.service.impl.AppVersionServiceImpl.java
 * <p>
 * 类产生时间: 2014-7-23 0023 下午 20:45
 * <p>
 * 负责人: 090922(陈伟)
 * <p>
 * Email:javacspring@gmail.com
 * <p>
 * 所在组 : 掌上公安应用平台
 * <p>
 * 所在部门: 开发部--手持技术部
 * <p>
 * <p>
 */
package com.oceansoft.mobile.econsole.modules.apps.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.oceansoft.mobile.econsole.cache.redis.CacheBaseService;
import com.oceansoft.mobile.econsole.common.util.web.PageUtil;
import com.oceansoft.mobile.econsole.modules.apps.entity.AppVersion;
import com.oceansoft.mobile.econsole.modules.apps.persistence.IAppVersionMapper;
import com.oceansoft.mobile.econsole.modules.apps.service.IAppVersionService;
import com.oceansoft.mobile.econsole.modules.base.entity.Pager;
import com.oceansoft.mobile.econsole.modules.base.entity.Query;
import com.oceansoft.mobile.econsole.modules.base.entity.Result;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.File;
import java.io.FileInputStream;
import java.util.Date;
import java.util.List;

/**
 * 应用版本业务组件实现类
 *
 * @author: chenw
 * @time: 2014-7-23 0023 下午 20:45
 */
@Service
public class AppVersionServiceImpl extends CacheBaseService implements IAppVersionService {

    @Resource
    private IAppVersionMapper appVersionMapper;

    @Override
    public Pager<AppVersion> findChannelByPage(Query query) {
        PageHelper.startPage(query.getPageNum(), query.getNumPerPage(), true);
        Page<AppVersion> page = (Page<AppVersion>) appVersionMapper.findVersionByPage(query);
        return PageUtil.page2pager(page, query);
    }

    @Override
    public Result loadIosLastVersion() {
        PageHelper.startPage(1, 1, true);
        Page<AppVersion> page = (Page<AppVersion>) appVersionMapper.loadIosLastVersion();
        if (null != page.getResult() && page.getResult().size() > 0) {
            AppVersion appVersion = page.getResult().get(0);
            return new Result(appVersion);
        }
        return new Result(Result.FAILURE, "更新错误");
    }

    @Override
    public Result loadLastVersion(int appId, int versionCode, String hostIP) {
        PageHelper.startPage(1, 1, true);
        Page<AppVersion> page = (Page<AppVersion>) appVersionMapper.loadLastVersion(appId, versionCode);
        if (null != page.getResult() && page.getResult().size() > 0) {
            AppVersion appVersion = page.getResult().get(0);
//            File file = new File(configInfo.getUploadSubDir(appVersion.getUrl()));
            //文件不存在时 从数据库读取并生成文件
/*            if (!file.exists()) {
                try {
                    File newFile = FileUtils.createFile(FileUtils.makeDir(configInfo.getUploadSubDir("app")), appVersion.getMd5() + ".apk");
                    FileUtils.createFile(new ByteArrayInputStream(appVersion.getAppContent()), newFile);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }*/
//            appVersion.setAppContent(null);
            appVersion.setUrl(configInfo.getResourceDownloadUrl(appVersion.getUrl()));
            return new Result(appVersion);
        }
        return new Result(Result.FAILURE, "当前已是最新版本");
    }

    @Override
    public Result loadAppVersionHistory(int appId) {
        List<AppVersion> list = appVersionMapper.loadVersionHistory(appId);
        if (null != list && list.size() > 0) {
            return new Result(list);
        }
        return new Result(Result.FAILURE, "暂无历史版本信息");
    }

    @Override
    public Result save(AppVersion version, String hostIP) {
        byte[] bytes = null;
        File file = new File(configInfo.getUploadSubDir(version.getUrl()));

        try {
            if (file.exists()) {
                bytes = new byte[(int) file.length()];
                FileInputStream inputStream = new FileInputStream(file);
                inputStream.read(bytes);
                inputStream.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        version.setAppContent(bytes);
        version.setPubTime(new Date());
        version.setAppId(1);
        version.setId(System.nanoTime());
        try {
            appVersionMapper.save(version);
        }catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(version);
    }
}
